home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Documentation / DirectX9 / directx9_c.chm / directx / code / glossary.js < prev    next >
Encoding:
JavaScript  |  2004-09-30  |  5.7 KB  |  208 lines

  1. // The method is called when the user positions the mouse cursor over a glossary term in a document.
  2. // Current implementation assumes the existence of an associative array (g_glossary). 
  3. // The keys of the array correspond to the argument passed to this function.
  4.  
  5. var bGlossary=true;
  6. var oDialog;
  7. var oTimeout="";
  8. var oTimein="";
  9. var iTimein=.5;
  10. var iTimeout=30;
  11. var oLastNode;
  12. var oNode;
  13. var bInit=false;
  14. var aTerms=new Array();
  15.  
  16. // Called from mouseover and when the contextmenu behavior fires oncontextopen.
  17. function clearDef(){
  18.     if((event)&&(event.toElement!=null)){
  19.         if(typeof(oTimein)=="number"){
  20.             window.clearTimeout(oTimein);
  21.         }
  22.         if(oDialog.dlg_status==true){
  23.             hideDef();
  24.         }
  25.     }
  26. }
  27. function hideDef(){
  28.     window.clearTimeout(oTimeout);
  29.     oTimeout="";
  30.     oDialog.style.display="none";
  31.     oDialog.dlg_status=false;    
  32. }
  33. function showDef(oSource){
  34.     if(bInit==false){
  35.         glossaryInit();
  36.         bInit=true;
  37.     }
  38.     if(bGlossary==true){
  39.         if(typeof(arguments[0])=="object"){
  40.             oNode=oSource;
  41.         }
  42.         else{
  43.             oNode=window.event.srcElement;
  44.         }
  45.         var bStatus=oDialog.dlg_status; // BUGBUG: oDialog is null.
  46.         if((oLastNode!=oNode)||(bStatus==false)){
  47.             if((typeof(oTimein)=="number")&&(event)&&(event.fromElement!=null)){
  48.                 window.clearTimeout(oTimein);
  49.             }
  50.             oTimein=window.setTimeout("openDialog(oNode)",iTimein*1000);
  51.         }    
  52.     }
  53. }
  54.  
  55.  
  56.  
  57. function glossaryInit(){
  58.         oDialog=fnCreateDialog(150,50);
  59. }
  60.  
  61. function navigateTerm(){
  62.     var oNode=event.srcElement;
  63.     var iTermID=oNode.termID;
  64.     if(oNode!=aTerms[iTermID]){
  65.         var iAbsTop=getAbsoluteTop(aTerms[iTermID]);
  66.         if(iAbsTop<document.body.scrollTop){
  67.             window.scrollTo(document.body.scrollLeft,getAbsoluteTop(aTerms[iTermID]));
  68.         }
  69.         openDialog(aTerms[iTermID]);
  70.     }
  71. }
  72. function disableGlossary(){
  73.     if(bGlossary==true){
  74.         event.srcElement.innerText="Enable Automatic Glossary";
  75.         bGlossary=false;
  76.         hideDef();        
  77.     }
  78.     else{
  79.         event.srcElement.innerText="Disable Automatic Glossary";
  80.         bGlossary=true;
  81.     }
  82. }
  83. function openGlossary(){
  84.  
  85. }
  86. function fnSetMenus(){
  87.     var oNode=event.srcElement;
  88.     var oMenu=oNode.createMenu("SPAN","G_RID");
  89.     var oSubItem1=oNode.createMenuItem("Glossary",fnStub,oMenu,true);
  90.     document.body.createMenuItem("Open External Glossary",openGlossary,oSubItem1.subMenu);
  91.     document.body.createMenuItem("Disable Automatic Glossary",disableGlossary,oSubItem1.subMenu);    
  92.     for(var i=0;i<aTerms.length;i++){
  93.         var oItem=document.body.createMenuItem(aTerms[i].innerText,navigateTerm,oMenu);
  94.         oItem.termID=i;
  95.     }
  96. }
  97. // This is a bogus stub.  It should be sniffed out rather than added in.
  98. function fnStub(){
  99.  
  100. }
  101. function fnAttachMenus(aTips){
  102.     // This walk is only necessary for the context menu.
  103.     var aTips=document.body.all.tags("SPAN");
  104.     for(var i=0;i<aTips.length;i++){
  105.         var oNode=aTips[i];
  106.         if(oNode.getAttribute("G_RID")){
  107.             var sTerm=oNode.getAttribute("G_RID");
  108.             if(typeof(g_glossary[sTerm])=="string"){
  109.                 // Removed client-side scripting to add events.  This entire process should be singled out for IE 5 and later .. and, its only for the context menu.
  110.                 aTerms[aTerms.length]=oNode;
  111.             }
  112.         }
  113.     }
  114.     if(oBD.majorVer>=5){
  115.         document.body.addBehavior(gsContextMenuPath);
  116.         document.body.onbehaviorready="fnSetMenus()";
  117.         document.body.oncontextopen="clearDef()";
  118.     }
  119.  
  120. }
  121. // Called by showDef.  The showDef function sniffs for initialization.
  122. function openDialog(oNode,x,y){
  123.      var bStatus=oDialog.dlg_status; // BUGBUG: This code assumes that oDialog has been initialized
  124.     if(bStatus==false){
  125.         oDialog.dlg_status=true;
  126.         oDialog.style.display="block";
  127.     }
  128.     else{
  129.         if(typeof(oTimeout)=="number"){
  130.             window.clearTimeout(oTimeout);
  131.         }
  132.     }
  133.     
  134.     var sTerm=oNode.getAttribute("G_RID");    
  135.     var oDef=oNode.children(0);
  136.     var sDef=oDef.text;
  137.     sDef=sDef.substr(4,sDef.length-7);    //Strips the html comment markers from the definition.
  138.     oDialog.innerHTML=sDef
  139.     
  140.     
  141.     //oDialog.innerHTML=g_glossary[sTerm];
  142.         
  143.     var iScrollLeft=document.body.scrollLeft;
  144.     var iScrollTop=document.body.scrollTop;
  145.     var iOffsetLeft=getAbsoluteLeft(oNode)// - iScrollLeft;
  146.     var iOffsetWidth=oNode.offsetWidth;
  147.     var oParent=oNode.parentElement;
  148.     var iOffsetParentLeft=getAbsoluteLeft(oParent);
  149.     var iOffsetTop=getAbsoluteTop(oNode); //- iScrollTop;
  150.     var iOffsetDialogWidth=oDialog.offsetWidth;
  151.     
  152.     
  153.     if((iOffsetLeft + iOffsetWidth) > (iOffsetParentLeft + oParent.offsetWidth)){
  154.         iOffsetLeft=iOffsetParentLeft;
  155.         if(iOffsetLeft - iOffsetDialogWidth>0){
  156.             iOffsetTop+=oNode.offsetHeight;
  157.         }
  158.     }
  159.     var iLeft=0;
  160.     var iTop=0;
  161.     if((iOffsetLeft + iOffsetWidth - iScrollLeft + iOffsetDialogWidth) < document.body.offsetWidth ){
  162.         iLeft=iOffsetLeft + iOffsetWidth;
  163.     }
  164.     else{
  165.         if(iOffsetLeft - iOffsetDialogWidth>0){
  166.             iLeft=iOffsetLeft - iOffsetDialogWidth;
  167.         }
  168.         else{
  169.             iLeft=iOffsetParentLeft;
  170.         }
  171.     }
  172.     if(iOffsetTop - iScrollTop<oDialog.offsetHeight){
  173.         iTop=iOffsetTop + oNode.offsetHeight;
  174.     }
  175.     else{
  176.         iTop=iOffsetTop - oDialog.offsetHeight;
  177.     }
  178.     oDialog.style.top=iTop;
  179.     oDialog.style.left=iLeft;
  180.     oTimeout=window.setTimeout("hideDef()",iTimeout*1000);    
  181. }
  182. function getAbsoluteTop(oNode){
  183.     var oCurrentNode=oNode;
  184.     var iTop=0;
  185.     while(oCurrentNode.tagName!="BODY"){
  186.         iTop+=oCurrentNode.offsetTop;
  187.         oCurrentNode=oCurrentNode.offsetParent;
  188.     }
  189.     return iTop;
  190. }
  191. function getAbsoluteLeft(oNode){
  192.     var oCurrentNode=oNode;
  193.     var iLeft=0;
  194.     while(oCurrentNode.tagName!="BODY"){
  195.         iLeft+=oCurrentNode.offsetLeft;
  196.         oCurrentNode=oCurrentNode.offsetParent;
  197.     }
  198.     return iLeft;
  199. }
  200. function fnCreateDialog(iWidth,iHeight){
  201.     document.body.insertAdjacentHTML("BeforeEnd","<DIV></DIV>");
  202.     oNewDialog=document.body.children(document.body.children.length-1);
  203.     oNewDialog.className="clsTooltip";
  204.     oNewDialog.style.width=iWidth;
  205.     oNewDialog.dlg_status=false;
  206.     return oNewDialog;
  207. }
  208.